package s.k.gui.webtest.basetools;

import java.awt.event.KeyEvent;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

import s.k.gui.webtest.Allocator;
import s.k.gui.webtest.BrowserType;
import s.k.gui.webtest.BrowserWindow;
import s.k.gui.webtest.SeleniumDriver;
import s.k.gui.webtest.WebDriverBrowserWindow;
import s.k.gui.webtest.log.MyLogger;

public class SKWindow {

	protected static MyLogger logger = MyLogger.getLogger(SKWindow.class);

	static {

		SKBaseTools.configLogProperties();

	}

	/*
	 * 关闭所有的WINDOWS
	 */
	public static void closeAllWindows() {

		Allocator.getInstance().closeAllWindows();

	}

	public static void runJavaScript(String javascript) {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			try {

				JavascriptExecutor executor = (JavascriptExecutor) (WebDriver) currentWindow
						.getDriver().getEngine();

				executor.executeScript(javascript);

				logger.info(javascript + "执行成功");

			} catch (Exception e) {

				logger.error(javascript + "执行失败");
			}
		}
	}
	
	/**
	 * 在当前窗口执行这段js，传入的参数为paras,只对webdriver类型的窗口生效
	 * @param javascript 待执行的javascript脚本
	 * @param paras 执行js时候传入的参数列表
	 * */
	public static void runJavaScript(String javascript,Object...paras){
		BrowserWindow currentWindow=Allocator.getInstance().currentWindow;
		if(currentWindow instanceof WebDriverBrowserWindow){
			try{
				JavascriptExecutor executor=(JavascriptExecutor)(WebDriver)currentWindow.getDriver().getEngine();
				executor.executeScript(javascript,paras);
				logger.info(javascript+"执行成功!");
			}catch (Exception e) {
				logger.error(javascript+"执行失败!");
			}
		}
	}

	public static void moveWindowTo(int xLocation, int yLocation) {

		Allocator.getInstance().currentWindow
				.moveWindowTo(xLocation, yLocation);

	}

	public static void resizeTo(int horizonSize, int verticalSize) {

		Allocator.getInstance().currentWindow.resizeTo(horizonSize,
				verticalSize);

	}

	public static String getUrl() {

		return Allocator.getInstance().currentWindow.getUrl();

	}

	public static void refresh() {

		Allocator.getInstance().currentWindow.refresh();

	}

	public static void close() {

		Allocator.getInstance().currentWindow.close();

	}

	public static void goForward() {

		Allocator.getInstance().currentWindow.goForward();

	}

	public static void goBack() {

		Allocator.getInstance().currentWindow.goBack();

	}

	public static BrowserType getEngineType() {

		return Allocator.getInstance().currentWindow.getEngineType();

	}

	public static SeleniumDriver getDriver() {

		return Allocator.getInstance().currentWindow.getDriver();

	}

	public static void maxSizeWindow() {

		Allocator.getInstance().currentWindow.maxSizeWindow();

	}

	public static void openNewWindow(String url) {

		Allocator.getInstance().addAndSetBrowserWindow(BrowserType.WebDriverIE);

		Allocator.getInstance().currentWindow.open(url);

	}

	public static void openNewWindow(BrowserType browserType, String url) {

		Allocator.getInstance().addAndSetBrowserWindow(browserType);

		Allocator.getInstance().currentWindow.open(url);

	}

	public static void attach(String url) {

		Allocator.getInstance().attach(url);

	}

	public static void attachByContains(String url) {

		Allocator.getInstance().attachByContains(url);

	}

	public static void attachByRegular(String url) {

		Allocator.getInstance().attachByRegular(url);

	}

	public static void shiftKeyDown() {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyDown(Keys.SHIFT);

		}
	}

	public static void ctrlKeyDown() {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyDown(Keys.CONTROL);

		}
	}

	public static void altKeyDown() {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyDown(Keys.ALT);

		}
	}

	public static void KeyDown(KeyEvent key) {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyDown(Keys.valueOf(Character.toString(key.getKeyChar())));
		}
	}

	public static void shiftKeyUp() {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyUp(Keys.SHIFT);
		}
	}

	public static void ctrlKeyUp() {
		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyUp(Keys.CONTROL);
		}
	}


	public static void altKeyUp() {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyUp(Keys.ALT);
		}
	}

	public static void KeyUp(KeyEvent key) {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Actions action = new Actions((WebDriver) currentWindow.getDriver()
					.getEngine());

			action.keyUp(Keys.valueOf(Character.toString(key.getKeyChar())));
		}
	}

	public static void scrollToTop() {

		String TO_TOP = "window.scrollto(0,0)";

		SKWindow.runJavaScript(TO_TOP);

		logger.info("移动到了页面顶端！");

	}

	public static void scrollToBottom() {

		String TO_Bottom = "window.scrollTo(0,document.body.scrollHeight)";

		SKWindow.runJavaScript(TO_Bottom);
		
		logger.info("移动到了底部！");

	}

	public static void scrollTo(int xLocation, int yLocation) {

		String TO_LOCATION = "window.scrollTo(" + xLocation + "," + yLocation
				+ ");";

		SKWindow.runJavaScript(TO_LOCATION);

	}

	public static void upLoad(String localFilePath) {

	}

	public static void downLoad(String localFilePath) {

	}

	public static String dealAlert() {

		return Allocator.getInstance().currentWindow.dealAlert();

	}


	public static String dealPrompt(String input, boolean isYes) {

		return Allocator.getInstance().currentWindow.dealPrompt(input, isYes);

	}


	public static String dealConfirm(boolean isYes) {

		return Allocator.getInstance().currentWindow.dealConfirm(isYes);

	}

	public static void TakeScreenshot() {

		Allocator.getInstance().currentWindow.TakeScreenshot(System
				.currentTimeMillis() + ".png");

	}
}
